package h0907;

import java.util.Deque;
import java.util.LinkedList;

public class MyQueue {
    Deque<Integer> stack1=new LinkedList<>();
    Deque<Integer> stack2=new LinkedList<>();

    public void inQueue(int x){
        stack1.push(x);
    }
    public int deQueue(){
        if (stack1.isEmpty()){
            throw new RuntimeException("队列为空");
        }
        while (!stack1.isEmpty()){
            stack2.push(stack1.pop());
        }
        return stack2.pop();
    }
    public int peek(){
        if (stack2.isEmpty()){
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        //只有两个都空，报异常
        if (stack2.isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return stack2.peek();
    }

    public static void main(String[] args) {
        MyQueue queue=new MyQueue();
        queue.inQueue(2);
        queue.inQueue(3);
        queue.inQueue(4);
        System.out.println(queue.deQueue());
        System.out.println(queue.peek());
    }
}
